package com.dongdongshop.service.impl;

import com.alibaba.fastjson.JSON;
import com.dongdongshop.mapper.TbContentMapper;
import com.dongdongshop.model.TbContent;
import com.dongdongshop.model.TbContentExample;
import com.dongdongshop.service.ContentService;
import com.dongdongshop.util.RedisConstant;
import com.dongdongshop.vo.ContentVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ContentServiceImpl implements ContentService {
    @Resource
    private TbContentMapper contentMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<ContentVO> getAllContent() {
        List<TbContent> tbContents = contentMapper.selectByExample(null);
        List<ContentVO> collect = tbContents.stream().map(centent -> {
            ContentVO vo = new ContentVO();
            BeanUtils.copyProperties(centent, vo);
            return vo;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public int addContent(ContentVO contentVO) {
        deleteRedis(contentVO);
        TbContent content = new TbContent();
        BeanUtils.copyProperties(contentVO, content);
        int i = contentMapper.insertSelective(content);
        return i;
    }


    @Override
    public ContentVO getContentById(Long id) {
        TbContent content = contentMapper.selectByPrimaryKey(id);
        ContentVO contentVO = new ContentVO();
        BeanUtils.copyProperties(content, contentVO);
        return contentVO;
    }

    @Override
    public int updateContent(ContentVO contentVO) {
        deleteRedis(contentVO);
        TbContent content = new TbContent();
        BeanUtils.copyProperties(contentVO, content);
        int i = contentMapper.updateByPrimaryKeySelective(content);
        return i;
    }

    @Override
    public int deleteBatch(List<Long> ids) {
        TbContentExample example = new TbContentExample();
        TbContentExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(ids);
        int i = contentMapper.deleteByExample(example);
        deleteRedisAll();
        return i;
    }

    private void deleteRedisAll() {
        redisTemplate.delete(RedisConstant.INTEREST);
        redisTemplate.delete(RedisConstant.BANNER);
        redisTemplate.delete(RedisConstant.FAVORITE);
        redisTemplate.delete(RedisConstant.RECOMMEND);
    }


    @Override
    public List<ContentVO> getAdvertisingByOrder(Integer flag) {
        //轮播图
        if (flag == 1) {
            return getBanner();
        } else if (flag == 2) {//2猜你喜欢
            return getFavorite();
        } else if (flag == 3) {//3推荐
            return getRecommend();
        } else if (flag == 4) {//4有趣
            return getInterest();
        }
        return null;
    }

    private List<ContentVO> getInterest() {
        List<ContentVO> voList = null;
        String con = (String) redisTemplate.boundValueOps(RedisConstant.INTEREST).get();
        if (con != null) {
            voList = JSON.parseArray(con, ContentVO.class);
            return voList;
        }
        List<TbContent> contentList = contentMapper.getIntersertByOrder();
        voList = contentList.stream().map(content -> {
            ContentVO contentVO = new ContentVO();
            BeanUtils.copyProperties(content, contentVO);
            return contentVO;
        }).collect(Collectors.toList());
        redisTemplate.boundValueOps(RedisConstant.INTEREST).set(JSON.toJSONString(voList));
        return voList;
    }

    private List<ContentVO> getRecommend() {
        List<ContentVO> voList = null;
        String con = (String) redisTemplate.boundValueOps(RedisConstant.RECOMMEND).get();
        if (con != null) {
            voList = JSON.parseArray(con, ContentVO.class);
            return voList;
        }
        List<TbContent> contentList = contentMapper.getRecommendByOrder();
        voList = contentList.stream().map(content -> {
            ContentVO contentVO = new ContentVO();
            BeanUtils.copyProperties(content, contentVO);
            return contentVO;
        }).collect(Collectors.toList());
        redisTemplate.boundValueOps(RedisConstant.RECOMMEND).set(JSON.toJSONString(voList));
        return voList;
    }

    private List<ContentVO> getFavorite() {
        List<ContentVO> voList = null;
        String con = (String) redisTemplate.boundValueOps(RedisConstant.FAVORITE).get();
        if (con != null) {
            voList = JSON.parseArray(con, ContentVO.class);
            return voList;
        }
        List<TbContent> contentList = contentMapper.getFavoriteByOrder();
        voList = contentList.stream().map(content -> {
            ContentVO contentVO = new ContentVO();
            BeanUtils.copyProperties(content, contentVO);
            return contentVO;
        }).collect(Collectors.toList());
        redisTemplate.boundValueOps(RedisConstant.FAVORITE).set(JSON.toJSONString(voList));
        return voList;
    }

    private List<ContentVO> getBanner() {
        List<ContentVO> voList = null;
        String con = (String) redisTemplate.boundValueOps(RedisConstant.BANNER).get();
        if (con != null) {
            voList = JSON.parseArray(con, ContentVO.class);
            return voList;
        }
        List<TbContent> contentList = contentMapper.getBannerByOrder();
        voList = contentList.stream().map(content -> {
            ContentVO contentVO = new ContentVO();
            BeanUtils.copyProperties(content, contentVO);
            return contentVO;
        }).collect(Collectors.toList());
        redisTemplate.boundValueOps(RedisConstant.BANNER).set(JSON.toJSONString(voList));
        return voList;
    }

    private void deleteRedis(ContentVO contentVO) {
        Long categoryId = contentVO.getCategoryId();
        if (categoryId == 1) {
            redisTemplate.delete(RedisConstant.BANNER);
        } else if (categoryId == 2) {
            redisTemplate.delete(RedisConstant.FAVORITE);
        } else if (categoryId == 3) {
            redisTemplate.delete(RedisConstant.RECOMMEND);
        } else if (categoryId == 4) {
            redisTemplate.delete(RedisConstant.INTEREST);
        }
    }


}
